package com.chima.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.chima.domain.SonPost;

public class SonPostDao extends HibernateDaoSupport {
	

	//分页查询
			public List<SonPost> findByCriteria(DetachedCriteria criteria, int firstResult,int maxResults) {
				return this.getHibernateTemplate().findByCriteria(criteria,firstResult,maxResults);
			}
		   
			//查找总页数
			public int findByTotalCount(int depart) {
				long temp=231231l;
//				List list= this.getHibernateTemplate().find("select count(*) from SonPost");
//				temp = (Long) list.get(0);
				if(depart==0){
					List list= this.getHibernateTemplate().find("select count(*) from SonPost");
					temp = (Long) list.get(0);
				}else{
					List list= this.getHibernateTemplate().find("select count(*) from SonPost p where p.department.id='"+depart+"' ");
					 temp = (Long) list.get(0);
				}
			
				return (int) temp;
			}
			public void save(SonPost sonPost) {
				this.getHibernateTemplate().save(sonPost);
			}
			public void update(SonPost sonPost) {
				this.getHibernateTemplate().update(sonPost);
			}
			
			public void delete(int id) {
				this.getHibernateTemplate().delete(this.getHibernateTemplate().load(SonPost.class, id));
			}
			public List<SonPost> findPostByDept(int deptID){
				Session session   =this.getSession();
				List<SonPost> p = session.createQuery("from SonPost p where p.department.id = ?").setParameter(0, deptID).list();
				super.releaseSession(session);
				return p;
				
			}
			public SonPost findPostByName(String name){
				Session session   =this.getSession();
				SonPost p = (SonPost)session.createQuery("from SonPost p where p.name = ?").setParameter(0,name).uniqueResult();
				super.releaseSession(session);
				return p;
				
			}
			public SonPost findPostByNameAnddept(String name, int dept){
				Session session   =this.getSession();
				SonPost p = (SonPost)session.createQuery("from SonPost p where p.name = ? and p.department.id = ? ").setParameter(0,name).setParameter(1,dept).uniqueResult();
				super.releaseSession(session);
				return p;
				
			}
			public SonPost findPostById(int id){
				Session session   =this.getSession();
				SonPost p = (SonPost)session.createQuery("from SonPost p where p.id= ?").setParameter(0,id).uniqueResult();
				super.releaseSession(session);
				return p;
				
			}
			public List<SonPost> findsonPostBypId(int Pid){
				Session session   =this.getSession();
				 List<SonPost> p = session.createQuery("from SonPost p where p.post.id= ?").setParameter(0,Pid).list();
				super.releaseSession(session);
				return p;
				
			}


}
